package mume.scanning;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import mume.music.Scraper;

import android.content.Context;

public class ASINfinder extends Scraper{
	//gebruikte paramaters
	private static final String SERVICE = "AWSECommerceService";
	private static final String OPERATION = "ItemLookup";
	private static final String RESPONSE_GROUP = "Large";
	private static final String MERCHANT_ID = "All";
	private static final String SEARCH_INDEX = "All";
	private static final String ID_TYPE = "EAN";
	
	Context context;
	
	public ASINfinder(Context context)
	{
		super(context);
	}
	
	public String getASIN( String barcode ) {
		String asin = "No ASIN found";
		String requestUrl = getRequestURL( barcode );

		capturedList = new Vector<String>();
	
        String start = "<ASIN>";
        char end = '<';
        String offset = "<Item>";
        int max = 1;
        
        initialize(requestUrl,start, end, offset, max );
       
        if(searchForData() && capturedList.size() > 0)
    		asin = capturedList.firstElement();
        
		return asin;
	}
	
	private String getRequestURL( String barcode )
	{
		Map<String, String> params = new HashMap<String, String>();
		SignedRequestsHelper signedRequestHelper = null;
		try {
			signedRequestHelper = new SignedRequestsHelper();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = "";
		
		params.put("IdType", ID_TYPE);
		params.put("MerchantId", MERCHANT_ID);
		params.put("Operation", OPERATION);
		params.put("ResponseGroup", RESPONSE_GROUP);
		params.put("SearchIndex", SEARCH_INDEX);
		params.put("Service", SERVICE);
		params.put("ItemId", barcode.replaceAll(" ", ""));
		
		url = signedRequestHelper.sign(params);
	
		return url;
	}


}
